package com.hirisun.wicket.link;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import org.apache.wicket.IClusterable;
import org.apache.wicket.markup.html.WebPage;
import org.apache.wicket.markup.html.basic.Label;
import org.apache.wicket.markup.html.form.CheckBox;
import org.apache.wicket.markup.html.form.CheckBoxMultipleChoice;
import org.apache.wicket.markup.html.form.ChoiceRenderer;
import org.apache.wicket.markup.html.form.Form;
import org.apache.wicket.markup.html.form.IChoiceRenderer;
import org.apache.wicket.markup.html.form.SubmitLink;
import org.apache.wicket.markup.html.form.TextField;
import org.apache.wicket.markup.html.link.BookmarkablePageLink;
import org.apache.wicket.markup.html.link.ExternalLink;
import org.apache.wicket.markup.html.link.Link;
import org.apache.wicket.markup.html.panel.FeedbackPanel;
import org.apache.wicket.model.CompoundPropertyModel;
import org.apache.wicket.model.PropertyModel;
import org.apache.wicket.request.mapper.parameter.PageParameters;

import com.hirisun.wicket.HelloWorld;

public class LinkPage extends WebPage {
	/**
	 * 
	 */
	private static final long serialVersionUID = -593297929419295183L;
	/**
	 * 
	 */
	private String website;
	
	private boolean bl;
	private String textarea;
	private static  List< String> list = new ArrayList<String>();
	private static Map<String, String> map = new HashMap<String, String>();
	
	static{
		list.add("http://www.baidu.com");
		list.add("http://www.sina.com.cn");
		list.add("http://www.163.com");
		
		
		map.put(list.get(0),"百度");
		map.put(list.get(1), "新浪");
		map.put(list.get(2), "网易");
	}
	
	
	
	
public static List<String> getList() {
		return list;
	}

	public static void setList(List<String> list) {
		LinkPage.list = list;
	}

public String getWebsite() {
		return website;
	}

	public void setWebsite(String website) {
		this.website = website;
	}

public String getTextarea() {
		return textarea;
	}

	public void setTextarea(String textarea) {
		this.textarea = textarea;
	}

	//	 
	private  int count =0;
//	
//	public int getCount() {
//		return count;
//	}
//
//	public void setCount(int count) {
//		this.count = count;
//	}

	public LinkPage() {
		super();
	      // input 
	      final Input input = new Input();
	        setDefaultModel(new CompoundPropertyModel<Input>(input));
		@SuppressWarnings("rawtypes")
		final
		
		Link link  = new Link ("link") {
			/**
			 * 
			 */
			private static final long serialVersionUID = 6861958909033086497L;

			@Override
			public void onClick() {
			
//			count++;
				
				 
			}
		};
		 link.add(new Label("count",  new PropertyModel<String>(this, "count")));
		
		 add(new ExternalLink("externalLink", "http://www.baidu.com", "百度"));
		 
		 PageParameters parms = new PageParameters();
		 parms.add("name", "yangruixi");
		BookmarkablePageLink<Object> bookMarkablePageLink = new BookmarkablePageLink<Object>("bookMarkablePageLink",HelloWorld.class,parms );
		 add(bookMarkablePageLink);
		 
		 //form
		  Form<Object> form = new Form<Object>("form"){
			  @Override
			protected void onSubmit() {
				info("form default sumbit");
			}
		  };
		  form.add( new org.apache.wicket.markup.html.form.Button("button1"){
			  @Override
			public void onSubmit() {
					info("form button1 sumbit");
			}
		  });
		  
		  form.add( new org.apache.wicket.markup.html.form.Button("button2"){
			  @Override
			public void onSubmit() {
					info("form button2 sumbit");
			}
		  }.setDefaultFormProcessing(false));
		  add(new FeedbackPanel("feedback"));
		  add(form);
		  //submitLink
		  
		  Form f = new Form("linkForm", new CompoundPropertyModel(textarea));
		     f.add(new TextField("value1"));
		     f.add(new SubmitLink("link1") {
		         public void onSubmit() {
		             System.out.println("Link1 was clicked, value1 is: "+textarea) ;
		            info("Link1 was clicked, value1 is: "+bl) ;

		         };
		      });
//		     f.add(new PasswordTextField("password"));
//		     f.add(new RequiredTextField<String>("name"));
		      add(new SubmitLink("link2",f) {
		          public void onSubmit() {
		              System.out.println("Link2 was clicked, value1 is: "+textarea);
		              info("Link1 was clicked, value1 is: "+bl) ;
		    
		              info("input: " + input);

		           };
		      });
				 //textarea
		      f.add(new CheckBox("checkbox",new PropertyModel<Boolean>(this, "bl")));
		      f.add(  new org.apache.wicket.markup.html.form.TextArea<String>("textarea", new PropertyModel<String>(this, "textarea")));
		      //checkboxmultichoice
		       
		      IChoiceRenderer<String > render = new ChoiceRenderer<String>(){
		    	   @Override
		    	public Object getDisplayValue(String object) {
		    		// TODO Auto-generated method stub
		    		return	 map.get(object);
		    	}
		      };
		 
		      
		      

		        
		        f.add(new CheckBoxMultipleChoice<String>("sites", list,render) );

		      
		  add(f);

		 
		add(link);
	}
	
	
	
	 private static class Input implements IClusterable
	    {
	        /** the selected sites. */
	        public List<String> sites = new ArrayList<String>();

	        /** the selected choices. */
	        public List<String> choices = new ArrayList<String>();

	        /** adds pre-selected items to the choices list */
	        public Input()
	        {
	            choices.add("http://www.baidu.com");
//	            choices.add("Choice4");
//	            choices.add("Choice5");
	        }

	        /**
	         * @see java.lang.Object#toString()
	         */
	        @Override
	        public String toString()
	        {
	            return "sites = '" + listAsString(sites) + "', choices='" + listAsString(choices) + "'";
	        }

	        private String listAsString(List<String> list)
	        {
	         StringBuilder b = new StringBuilder();
	            for (Iterator<String> i = list.iterator(); i.hasNext();)
	            {
	                b.append(i.next());
	                if (i.hasNext())
	                {
	                    b.append(", ");
	                }
	            }
	            return b.toString();
	        }
	    }


}
